package com.spinach.game.events.impl;

import com.alibaba.fastjson2.JSONObject;
import com.spinach.game.core.GameMessage;
import com.spinach.game.enums.ClientEventCode;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.stereotype.Service;

@Service
public class BaseServerEvent {

    /**
     * 发送用户不存在的消息
     * @param channel
     * @param userId 用户id
     */
    public void sendUserDoesNotExist(Channel channel, Long userId) {
        GameMessage produceMessage = GameMessage.builder()
                //客户端事件名称
                .code(ClientEventCode.CODE_USER_DOES_NOT_EXIST.name())
                //返回不存在的房间id
                .data(userId)
                .build();
        channel.writeAndFlush(new TextWebSocketFrame(JSONObject.toJSONString(produceMessage)));
    }

    /**
     * 发送房间不存在的消息
     * @param channel
     * @param roomId 房间号
     */
    public void sendRoomDoesNotExist(Channel channel, Long roomId) {
        GameMessage produceMessage = GameMessage.builder()
                //客户端事件名称
                .code(ClientEventCode.CODE_ROOM_DOES_NOT_EXIST.name())
                .data(roomId)
                .build();
        channel.writeAndFlush(new TextWebSocketFrame(JSONObject.toJSONString(produceMessage)));
    }

    public void sendRoomFull(Channel channel, Long roomId) {
        GameMessage produceMessage = GameMessage.builder()
                //客户端事件名称
                .code(ClientEventCode.CODE_ROOM_FULL.name())
                .data(roomId)
                .build();
        channel.writeAndFlush(new TextWebSocketFrame(JSONObject.toJSONString(produceMessage)));
    }

    public void sendRoomPlaying(Channel channel, Long roomId) {
        GameMessage produceMessage = GameMessage.builder()
                //客户端事件名称
                .code(ClientEventCode.CODE_ROOM_PLAYING.name())
                .data(roomId)
                .build();
        channel.writeAndFlush(new TextWebSocketFrame(JSONObject.toJSONString(produceMessage)));
    }

}
